import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @作者 ： pwx5328113 @时间： 2023/3/6
 * @测试点：jdbc连接参数autoBalance为leastconn，enableQuickAutoBalance设置为true/ false，连接节点为单节点
 **/
public class JDBC_QuikAutoBalance_Case0008 {
	public static void main(String[] args) throws Exception {
		System.out.println("----------start----------");
		List<Connection> connections = new ArrayList<>();
		System.out.println("----------point1：jdbc连接参数autoBalance为leastconn，enableQuickAutoBalance设置为true----------");
		System.out.println("----------建立10个连接----------");
		String url1 = "jdbc:postgresql://" + args[0] + "/" + args[1]
				+ "?loggerLevel=info&autoBalance=leastconn&enableQuickAutoBalance=true";
		for (int i = 0; i < 10; i++) {
			try {
				Connection connection = DriverManager.getConnection(url1, args[2], args[3]);
				connections.add(connection);
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
		System.out.println("----------sleep 10s----------");
		// sleep过程中数据库节点查询建连数量
		TimeUnit.SECONDS.sleep(10);
		System.out.println("----------connections close start----------");
		for (Connection connection : connections) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------connections close end----------");

		System.out.println("----------point2：jdbc连接参数autoBalance为leastconn，enableQuickAutoBalance设置为false----------");
		System.out.println("----------建立5个连接----------");
		String url2 = "jdbc:postgresql://" + args[0] + "/" + args[1]
				+ "?loggerLevel=info&autoBalance=leastconn&enableQuickAutoBalance=false";
		for (int i = 0; i < 5; i++) {
			try {
				Connection connection = DriverManager.getConnection(url2, args[2], args[3]);
				connections.add(connection);
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
		System.out.println("----------sleep 10s----------");
		// sleep过程中数据库节点查询建连数量
		TimeUnit.SECONDS.sleep(10);
		System.out.println("----------connections close start----------");
		for (Connection connection : connections) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------connections close end----------");
	}
}
